#include "CustomRectItem.h"
#include <QPen>
#include <QGraphicsScene>
#include <QApplication>
#include <QPainter>
#include <QTransform>
CustomRectItem::CustomRectItem(QGraphicsItem* parent)
    : QGraphicsRectItem(parent), 
    isDragging(true),
    isResizing(true),
    resizeHandleSize(10)
{
    // 设置初始矩形大小和位置
    setRect(0, 0, 100, 100);
    setBrush(Qt::blue);
}

CustomRectItem::CustomRectItem(const QRectF& rect, QGraphicsItem* parent) : QGraphicsRectItem(rect, parent)
{
    // 允许选择和移动
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);

    // 设置可调整大小的边框
    setAcceptHoverEvents(true);
    QPen pen("red");
    setPen(pen);
}

CustomRectItem::CustomRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem* parent)
    :QGraphicsRectItem(x,y,w,h,parent)
{
    setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges | ItemIsFocusable);
}

//QVariant CustomRectItem::itemChange(GraphicsItemChange change, const QVariant& value)
//{
//    if (change == QGraphicsItem::ItemPositionChange && scene()) {
//        // 确保矩形保持在场景边界内
//        QRectF sceneRect = scene()->sceneRect();
//        QPointF newPos = value.toPointF();
//        newPos.setX(std::max(newPos.x(), sceneRect.left()));
//        newPos.setY(std::max(newPos.y(), sceneRect.top()));
//        newPos.setX(std::min(newPos.x(), sceneRect.right() - rect().width()));
//        newPos.setY(std::min(newPos.y(), sceneRect.bottom() - rect().height()));
//        return newPos;
//    }
//    return QGraphicsRectItem::itemChange(change, value);
//}

void CustomRectItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        // 判断是否点击在缩放手柄上
        if (isResizeHandle(event->pos()))
        {
            isResizing = true;
            resizeStartPos = event->pos();
            resizeStartRect = rect();
        }
        else
        {
            isDragging = true;
            dragStartPos = event->pos();
        }

        QGraphicsRectItem::mousePressEvent(event);
    }
}

void CustomRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
    if (isDragging)
    {
        // 拖拽移动
        QPointF delta = event->pos() - dragStartPos;
        setPos(pos() + delta);
        dragStartPos = event->pos();
    }
    else if (isResizing)
    {
        // 调整大小
        QPointF delta = event->pos() - resizeStartPos;
        QRectF newRect = resizeStartRect;

        // 根据鼠标位置调整矩形大小
        if (event->pos().x() > rect().right() - resizeHandleSize)
        {
            newRect.setWidth(rect().width() + delta.x());
        }
        if (event->pos().y() > rect().bottom() - resizeHandleSize)
        {
            newRect.setHeight(rect().height() + delta.y());
        }
        if (event->pos().x() < rect().left() + resizeHandleSize)
        {
            newRect.setLeft(rect().left() + delta.x());
            newRect.setWidth(rect().width() - delta.x());
        }
        if (event->pos().y() < rect().top() + resizeHandleSize)
        {
            newRect.setTop(rect().top() + delta.y());
            newRect.setHeight(rect().height() - delta.y());
        }

        // 设置新的矩形大小，并触发重绘
        setRect(newRect);
        resizeStartPos = event->pos();
    }

    QGraphicsRectItem::mouseMoveEvent(event);
}

void CustomRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        isDragging = false;
        isResizing = false;
    }

    QGraphicsRectItem::mouseReleaseEvent(event);
}

void CustomRectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    QGraphicsRectItem::paint(painter, option, widget);

    // 绘制缩放手柄（右下角）
    QRectF handleRect(rect().right() - resizeHandleSize, rect().bottom() - resizeHandleSize, resizeHandleSize, resizeHandleSize);
    painter->setBrush(Qt::red);
    painter->drawRect(handleRect);
}

//void CustomRectItem::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
//{
//    QRectF rect = this->rect();
//    QPointF pos = event->pos();
//    const qreal resizeMargin = 10;
//
//    // 右下角调整大小区域
//    if (pos.x() > rect.right() - resizeMargin &&
//        pos.y() > rect.bottom() - resizeMargin)
//    {
//        setCursor(Qt::SizeFDiagCursor);
//    }
//    else
//    {
//        setCursor(Qt::ArrowCursor);
//    }
//
//    QGraphicsRectItem::hoverMoveEvent(event);
//}
